<?php
class Category_Model extends CI_Model {

    /**
     * construct function
     */
    public  function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

    /**
     * Get list category root (parentId == 0)
     * @return <array>
     */
    public function getCategoryRoot()
    {
        //call query
        $query = $this->db->select('*')->from('category')->where('ParentID', 0)->order_by('CategoryID ASC')->get();

        //Get data form DB
        $data = $query->result_array();

        $categoryRoot[] = array(
           'CategoryID' => 0,
           'Name'       => '------- ROOT -------'
        );

        //If data NOT null
        if(!empty($data))
        {
            foreach($data as $category)
            {
                $categoryRoot[] = array(
                    'CategoryID' => $category['CategoryID'],
                    'Name'       => $category['Name']
                );
            }
        }

        //Return data
        return $categoryRoot;
    }

    /**
     * Get list category
     * @return <array>
     */
    public function getCategoryList()
    {
        //call query
        $query = $this->db->select('*')->from('category')->order_by('CategoryID ASC')->get();

        //Get data form DB
        $data = $query->result_array();

        /*If data NOT null
        if(!empty($data))
        {
            foreach($data as $category)
            {
                //Check parent
                if($category['ParentID'] != 0)
                {
                    $categoryParent[$category['ParentID']] = $category['Name'];
                }
            }
        }*/

        //Return data
        return $data;
    }

    /**
     * Get category detail by categoryId
     * @param <int> $categoryId
     * @param <array>
     */
    public function getCategoryDetail($categoryId)
    {
        //call query
        $query = $this->db->select('*')->from('category')->where('CategoryID', $categoryId)->get();

        //Return data
        return $query->row_array();
    }

    /**
     * Insert new category
     * @param <array> $params
     * @return <boolean>
     */
    public function insertCategory($params)
    {
        //Insert to DB
        $result = $this->db->insert('category', $params);

        //Return
        return $result;
    }

    /**
     * Edit category by categoryId
     * @param <int> $categoryId
     * @param <array> $params
     * @return <boolean>
     */
    public function editCategory($categoryId, $params)
    {
        //Insert to DB
        $result = $this->db->where('CategoryID', $categoryId)->update('category', $params);

        //Return
        return $result;
    }

    /**
     * Delete category
     * @param <int> $categoryId
     * @return <boolean>
     */
    public function deleteCategory($categoryId)
    {
        //Delete
        $result = $this->db->where('CategoryID', $categoryId)->delete('category');

        //Return
        return $result;
    }
    
}
